<template>
  <div class="main">
    <h2>运营管理系统</h2>
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>请登录系统</span>
        </div>
      </template>
      <el-form
        ref="ruleFormRef"
        :model="form"
        :rules="rules"
        status-icon
        @submit.prevent
      >
        <el-form-item prop="username">
          <el-input
            size="large"
            v-model="form.username"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            size="large"
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item prop="captcha">
          <el-space>
            <el-input
              size="large"
              v-model="form.captcha"
              placeholder="验证码"
            />
            <Captcha ref="ref1"></Captcha>
          </el-space>

        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            style="width:100%"
            @click="onSubmit(ruleFormRef)"
            native-type="submit"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>

</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import http from '../../http/req'
import url from '@/config/url'
const router = useRouter()
const ruleFormRef = ref<FormInstance>()
const form = reactive({
  username: '',
  password: '',
  captcha: '',
  key: ''
})
const rules = reactive<FormRules>({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 12, message: '密码长度6-12位', trigger: 'blur' }
  ],
  captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
})
const ref1 = ref()
const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // console.log('submit!')
      form.key = ref1.value.key
      // console.log(form)
      http.post(url.Login, form).then((res) => {
        // console.log(res)
        if (res.data.errNo === 0) {
          ElMessage({
            showClose: true,
            message: '登录成功',
            type: 'success',
            duration: 1000,
            onClose: () => {
              router.push('/dashboard/index')
            }
          })
        } else {
          let errText = res.data.errText
          if (res.data.errNo === 100004) {
            errText = '用户名或者密码错误'
          }
          ElMessage({
            showClose: true,
            message: errText,
            type: 'error',
            duration: 1000,
            onClose: () => {
              // next('/login')
              formEl.resetFields()
              ref1.value.load()
            }
          })
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<style lang="scss" scoped>
.main {
  background-image: url('../../assets/images/bg.jpg');
  background-size: cover;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  > h2 {
    color: #fff;
    background-color: rgba(0, 0, 0, 0.4);
    padding: 5px 20px;
    border-radius: 10px;
  }
}
.card-header {
  display: flex;
  justify-content: center;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 20%;
}
</style>